<?php 
class Administrator_Model_Users extends Zend_Db_Table_Abstract{
	protected $_name = "ifunsoft_users";
	protected $_primary = "uid";
	protected $db;
	var $uid, $username, $fullname, $email, $gro_id, $password, $status = "Enable";

	public function __construct(){
		$this->db = Zend_Registry::get('db');
	}

	public function login(){
		$db = Zend_Registry::get('connectDB');
		$auth = Zend_Auth_Admin::getInstance();
		$authAdapter = new Zend_Auth_Adapter_DbTable($db);
		$authAdapter->setTableName($this->_name)
					->setIdentityColumn('username')
					->setCredentialColumn('password');
	  
		$authAdapter->setIdentity($this->username);
		$authAdapter->setCredential(md5(md5($this->password)));
		$select = $authAdapter->getDbSelect();
		$select->where("status = 'Enable'");
		$result = $auth->authenticate($authAdapter);
		if ($result->isValid()) {
			$data = $authAdapter->getResultRowObject(null, array('password'));
			$auth->getStorage()->write($data);

			return true;
		}
		return false;
	}
	
	public function getUser(){
		$query = $this->db->select();
		$query->from($this->_name);
		$query->join("ifunsoft_group","ifunsoft_group.gid = ifunsoft_users.gro_id",array("ifunsoft_group.name as name_group"));
		$query->where("ifunsoft_users.status <>?", 'Delete');
		return $this->db->fetchAll($query);
	}
	
	public function updateStatusUser(){
		try {
			$where = array("uid =?" => $this->uid);
			$data = array("status" => $this->status);
			$this->db->update($this->_name,$data,$where);
			return true;
		} catch (Exception $e) {
			return false;
		}
	}
	
	public function insertUser(){
		try {
			$data = array(
					"username" => $this->username,
					"fullname" => $this->fullname,
					"email" => $this->email,
					"gro_id" =>$this->gro_id,
					"password" => md5(md5($this->password)),
					"status" => $this->status
			);
			$this->db->insert($this->_name,$data);
			return true;
		} catch (Exception $e) {
			return false;
		}
	}
	public function updateUser(){
		try {
			$data = array(
					"fullname" => $this->fullname,
					"email" => $this->email,
					"gro_id" =>$this->gro_id
			);
			$where = array("uid =?" => $this->uid);
			$this->db->update($this->_name,$data, $where);
			return true;
		} catch (Exception $e) {
			return false;
		}
	}
	
	public function checkExistusername(){
		$query = $this->db->select();
		$query->from($this->_name);
		$query->where("username =?", $this->username);
		$result = $this->db->fetchRow($query);
		if($result){
			return true;
		}
		return $result;
	}
	public function checkExistEmail(){
		$query = $this->db->select();
		$query->from($this->_name);
		$query->where("email =?", $this->email);
		$result = $this->db->fetchRow($query);
		if($result){
			return true;
		}
		return $result;
	}
	
	public function checkUserEnable(){
		$query = $this->db->select();
		$query->from($this->_name);
		$query->where("username =?", $this->username);
		$query->where("status =?", "Enable");
		return $this->db->fetchRow($query);
	
	}
	
	public function checkExistUser(){
		$query = $this->db->select();
		$query->from($this->_name);
		$query->where("username =?", $this->username);
		$query->where("password =?", md5(md5($this->password)));
		return $this->db->fetchRow($query);
	
	}
	
	public function getByUid(){
		$query = $this->db->select();
		$query->from($this->_name);
		$query->where("uid =?", $this->uid);
		return $this->db->fetchRow($query);
	}
	
	public function changepassUser(){
		try {
			$data = array(
					"password" => md5(md5($this->password))
			);
			$where = array("uid =?" => $this->uid);
			$this->db->update($this->_name,$data, $where);
			return true;
		} catch (Exception $e) {
			return false;
		}
	}
}
?>